package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"os"
	"strings"
	"time"
)

var (
	year  int
	month int
)

func showCalendar() {
	now := time.Now()
	showYear := year
	if showYear == 0 {
		//默认使用今年
		showYear = int(now.Year())
	}
	showMonth := time.Month(month)
	if showMonth == 0 {
		showMonth = now.Month()
	}
	showTime := time.Date(showYear, showMonth, 1, 0, 0, 0, 0, now.Location())
	weekdays := []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
	for _, weekday := range weekdays {
		fmt.Printf("%5s", weekday)
	}
	fmt.Println()
	for {
		startWd := showTime.Weekday()
		fmt.Printf("%s", strings.Repeat(" ", int(startWd)+5))
		for ; startWd <= time.Saturday; startWd++ {
			fmt.Printf("%5d", showTime.Day())
			showTime = showTime.Add(time.Hour * 24)
			if showTime.Month() != showMonth {
				return
			}
		}
		fmt.Println()

	}
}

//dated respnset
// for
// now
// year
// moth
// year
// moth
// yime
// for
// started:= show.Week
// for ; staid<data.sta;
// showti.ad
// if showt.moth !=shoiwo
// rrtun
// 获取 1 2 3 4 56 + 24
// 2 3 4 5
// 5
// show.diwe
// shomonth 6
// 1 2 3 4
// 2 3 46
// 6
// 7
// show  1,o
var dateCmd = &cobra.Command{
	Use:   "date",
	Short: "Date show the moth calendar of the year",
	Run: func(cmd *cobra.Command, args []string) {
		if year < 1000 && year > 9999 {
			fmt.Fprintln(os.Stderr, "invalid year should in [1000,9999],actual:%d", year)
			os.Exit(1)
		}
		if month < 1 && month > 12 {
			fmt.Fprintln(os.Stderr, "invalid moth should in(1,12),actual :%d", month)
			os.Exit(1)
		}
		showCalendar()
	},
}

func init() {
	rootCmd.AddCommand(dateCmd)
}
